package com.acme.service.bank.impl;

import com.acme.domain.bank.Bank;
import com.acme.service.bank.BankCannotBeSavedException;
import com.acme.service.bank.BankSaver;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

public class BankSaverToFile implements BankSaver {
    @Override
    public void save(final Bank bank, final String pathTo) throws BankCannotBeSavedException {
        try (FileOutputStream fos = new FileOutputStream(pathTo)) {
            try (BufferedOutputStream bos = new BufferedOutputStream(fos)) {
                try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
                    oos.writeObject(bank);
                }
            }
        } catch (Exception e) {
            throw new BankCannotBeSavedException(e);
        }
    }
}
